<template>
  <div>
    <div ref="text">
    </div>

    <el-dialog :visible.sync="showUploadImage" width="460px">
      <div class="image-upload">
        <el-upload
          drag
          :action="imageAction"
          :headers="imageHeaders"
          :data="imageData"
          :accept="imageAccept"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Quill from 'quill'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import './style.scss'
export default {
  name: 'KEditor',
  props: {
    toolbar: {
      default: () => {
        return [
          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
          ['blockquote', 'code-block'],

          [{ 'header': 1 }, { 'header': 2 }],               // custom button values
          [{ 'list': 'ordered'}, { 'list': 'bullet' }],
          [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
          [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
          [{ 'direction': 'rtl' }],                         // text direction

          [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
          [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

          [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
          [{ 'font': [] }],
          [{ 'align': [] }],
          ['image'],

          ['clean']                                         // remove formatting button
        ]
      }
    },
    imageAction: {
      type: String,
      default: ''
    },
    imageHeaders: Object,
    imageData: Object,
    imageAccept: String
  },
  data () {
    return {
      editor: null,
      showUploadImage: false
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.editor = new Quill(this.$refs['text'], {
        debug: 'info',
        modules: {
          toolbar: this.toolbar
        },
        theme: 'snow'
      })
      let toolbar = this.editor.getModule('toolbar')
      toolbar.addHandler('image', (de, f) => {
        if (this.imageAction === '') {
          console.warn('image-action is null!')
          return
        }
        this.showUploadImage = true
        // this.editor.format('image', 'http://pic15.nipic.com/20110628/1369025_192645024000_2.jpg')
      })
    })
  },
  destroyed () {
  }
}
</script>

<style scoped>

</style>
